#include "StdAfx.h"
#include "xkRefPointMan.h"
#include "xhImgAnaly.h"

#include "DDFun.h"


xkRefPointMan::xkRefPointMan(void)
{
	m_pImgAnaly = (xhImgAnaly*)this;

	m_nTempType    = -1;
	m_pActivePoint = NULL;
	m_arImgRefPoint.reserve(20);

	GetModuleFileName(0, m_szAnalyDir, MAX_PATH);
	char *pTemp = strrchr( m_szAnalyDir,'\\' );
	pTemp[0] ='\0';
	sprintf(m_szAnalyDir,"%s\\%s",m_szAnalyDir,DD_DEFAULT_ANYDIR);
}

xkRefPointMan::~xkRefPointMan(void)
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	UnInitRefList();

}

void xkRefPointMan::UnInitRefList()
{
	m_pActivePoint = NULL;

	xkRefPoint *pPoint = NULL;
	int nCount = (int)m_arImgRefPoint.size();
	for(int i=0;i<nCount;i++)
	{
       pPoint = m_arImgRefPoint[i];
	   MySafeDelete(pPoint);
	}
	m_arImgRefPoint.clear();
}

void xkRefPointMan::UnInitialize()
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	UnInitRefList();
}

int xkRefPointMan::Initialize()
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	UnInitRefList();

	return 0;
}


int xkRefPointMan::SetAnalyDirectory(char *pAnalyDir)
{
	if( NULL == pAnalyDir || strlen(pAnalyDir)<4 )
	{
		return DD_ERR_PARAMETER;
	}

	if( strcmp( m_szAnalyDir,pAnalyDir ) == 0 )
	{
		return DD_ERR_SUCCESS;
	}

	DD_AUTO_LOCK xLock(&m_RefSect);
    UnInitRefList();
	sprintf(m_szAnalyDir,pAnalyDir);

	return 0;
}


int xkRefPointMan::SetTemplate(int nType,zkAnalyTempInfo *pInfo)
{
	if( nType < 0 )
	{
		return DD_ERR_ANALY_TEMPLATEINVALID;
	}
	if( m_nTempType == nType )
	{
		if( NULL!= pInfo )
		{
			memcpy( &m_xTempInfo,pInfo,sizeof(zkAnalyTempInfo) );
		}
		return DD_ERR_SUCCESS;
	}

	m_nTempType = nType;
	if( NULL!= pInfo )
	{
		memcpy( &m_xTempInfo,pInfo,sizeof(zkAnalyTempInfo) );
	}
	else
	{
		memset( &m_xTempInfo,0,sizeof(zkAnalyTempInfo) );
		//m_xTempInfo.nTempType = nType;
		if( nType < MAX_TEMP_COUNT )
		{
			memcpy( &m_xTempInfo,&(m_pImgAnaly->m_xTemplateInfos[nType]),sizeof(zkAnalyTempInfo) );
		}
	}
	
	//load all points image
    //LoadTemplate();

	return -1;
}

void xkRefPointMan::LoadTempPoints()
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	UnInitRefList();

	char szTempFile[MAX_PATH] = "";
    char szTempPath[MAX_PATH] = "";
	sprintf(szTempPath,"%s\\%03d\\*.%s",m_szAnalyDir,m_nTempType,DD_ANALYTEMPEXT);

	int         nRet   = DD_ERR_FAILURE;
	int         nCount = 0;
	int         nPoint = -1;
	xkRefPoint *pPoint = NULL;

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szTempPath,&filedata ); 
	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( strlen(filedata.cFileName)>0 ) 
			{ 
				sprintf(szTempFile,"%s\\%03d\\%s",m_szAnalyDir,m_nTempType,filedata.cFileName);			
				   
				if( NULL == pPoint )
				{
					pPoint = new xkRefPoint();
					pPoint->m_pRefPointMan = this;
				}
#ifdef DKQ_TEST_TEMP_TIME
				g_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadFile %s -----",szTempFile);
#endif
				nRet = pPoint->LoadFile( szTempFile );
#ifdef DKQ_TEST_TEMP_TIME
				g_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadFile ++++++");
#endif
				if( DD_ERR_SUCCESS == nRet )
				{
                    nPoint = pPoint->m_xPointInfo.nPoint;
					do 
					{
						nCount = (int)m_arImgRefPoint.size();
						if( nPoint < 0 )
						{
							break;
						}
						else if( nPoint < nCount )
						{
							if( NULL != m_arImgRefPoint[nPoint] )
							{
								break;;
							}
						}
						else
						{
							while( nCount <= nPoint ) 
							{
								m_arImgRefPoint.push_back(NULL);
								nCount = (int)m_arImgRefPoint.size();
							}
						}

						pPoint->m_pRefPointMan  = this;
						m_arImgRefPoint[nPoint] = pPoint;
						m_pActivePoint          = pPoint;


						pPoint = NULL;

					} while (false);
				}
			} 

		}while(FindNextFile(filehandle,&filedata)); 

		FindClose(filehandle); 
	} 

	MySafeDelete(pPoint);

	if( nCount <= 0 )
	{
       m_pImgAnaly->m_xDispRef.ClearScreen(0x07);
	}
}


int xkRefPointMan::GetPoints(zkAnalyPointInfo **ppPoint,int &nCount)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( m_nTempType < 0 ) return DD_ERR_ANALY_TEMPLATEINVALID;

	*ppPoint = NULL;
	nCount   = 0;

	int nRefCount = (int)m_arImgRefPoint.size();
	if( nRefCount <= 0 )
	{
		return DD_ERR_FAILURE;
	}

	zkAnalyPointInfo *pInfo = NULL;
	for(int i=0;i<nRefCount;i++)
	{
		if( NULL != m_arImgRefPoint[i] )
		{
			if( NULL == pInfo )
			{
                pInfo = &(m_arImgRefPoint[i]->m_xPointInfo);

				*ppPoint = pInfo;
			}
			else
			{
				pInfo->pNext = &(m_arImgRefPoint[i]->m_xPointInfo);
				pInfo = pInfo->pNext;
			}

			nCount++;
			pInfo->pNext = NULL;
		}
	}

	return 0;
}


int xkRefPointMan::SelectPoint(int nPoint,BOOL bShow)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	int nCount = (int)m_arImgRefPoint.size();
	if( nPoint < 0 || nPoint >= nCount )
	{
		return DD_ERR_PARAMETER;
	}

	m_pActivePoint = m_arImgRefPoint[nPoint];
	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	if( bShow )
	{	
		zkImgBitInfo *pImgInfo = NULL;
		m_pActivePoint->GetOrgImgBuffer( &pImgInfo );
		if( NULL != pImgInfo )
		{
			m_pImgAnaly->SetDefaultDispPalette(eAnalyWindow_Ref,pImgInfo );
			m_pImgAnaly->LoadRefDataToDisp();

			m_pImgAnaly->m_xDispSap.ClearScreen(0x07);
			m_pImgAnaly->m_xDispAdj.ClearScreen(0x07);
			m_pImgAnaly->m_xDispSub.ClearScreen(0x07);

			if( NULL != m_pImgAnaly->m_funMsgCallBack )
			{
				m_pImgAnaly->m_funMsgCallBack(AnalyMsg_DispRefImage,AnalyMsgState_Finish,0,0,m_pImgAnaly->m_pContext);
				m_pImgAnaly->m_funMsgCallBack(AnalyMsg_EdgeStatistics,AnalyMsgState_Finish,&(m_pActivePoint->m_xEdgeHead),sizeof(zkAnalyEdgeHead),m_pImgAnaly->m_pContext);
			}
		}
	}

	return DD_ERR_SUCCESS;
}
int xkRefPointMan::AddPointBuffer(int nPoint,zkImgBitInfo *pImgInfo,BOOL bShareMem)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( m_nTempType < 0 ) return DD_ERR_ANALY_TEMPLATEINVALID;
	
	xkRefPoint *pPoint = NULL;
	int nCount = (int)m_arImgRefPoint.size();
	if( nPoint < 0 )
	{
		return DD_ERR_PARAMETER;
	}
	if( nPoint < nCount )
	{
		pPoint = m_arImgRefPoint[nPoint];
		if( NULL != pPoint )
		{
			return DD_ERR_ANALY_POINTEXIST;
		}
	}
	else
	{
		while( nCount <= nPoint )
		{
			m_arImgRefPoint.push_back(NULL);
			nCount = (int)m_arImgRefPoint.size();
		}
	}

	int nRet = DD_ERR_FAILURE;
	do 
	{
		pPoint = new xkRefPoint();
		nRet = pPoint->SetImgBuffer(pImgInfo,FALSE/*bShareMem*/);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		pPoint->m_pRefPointMan      = this;
		pPoint->m_xPointInfo.nPoint = nPoint;
		m_arImgRefPoint[nPoint]     = pPoint;
		m_pActivePoint              = pPoint;

		m_pImgAnaly->SetDefaultDispPalette( eAnalyWindow_Ref,pImgInfo );
		m_pImgAnaly->LoadRefDataToDisp();

		if( NULL != m_pImgAnaly->m_funMsgCallBack )
		{
			m_pImgAnaly->m_funMsgCallBack(AnalyMsg_DispRefImage,AnalyMsgState_Finish,0,0,m_pImgAnaly->m_pContext);
		}

		return DD_ERR_SUCCESS;

	} while (false);

	MySafeDelete( pPoint );

	return nRet;
}

int xkRefPointMan::SavePoint()
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	if( m_nTempType < 0 ) return DD_ERR_ANALY_TEMPLATEINVALID;

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	int nPoint = m_pActivePoint->m_xPointInfo.nPoint;

	char szTempFile[MAX_PATH] = "";
    sprintf(szTempFile,"%s\\%03d\\%03d.%s",m_szAnalyDir,m_nTempType,nPoint,DD_ANALYTEMPEXT);

	DDCreateDeepDir( szTempFile );

	return m_pActivePoint->SaveFile(szTempFile);
}


int xkRefPointMan::GetPointIndex(int &nIndex)
{
	nIndex = -1;

	DD_AUTO_LOCK xLock(&m_RefSect);

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	nIndex = m_pActivePoint->m_xPointInfo.nPoint;

	return DD_ERR_SUCCESS;
}


int xkRefPointMan::SetAnalyCfg(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( m_nTempType < 0 ) return DD_ERR_ANALY_TEMPLATEINVALID;

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	return m_pActivePoint->SetAnalyCfg( pVoid );
}
int xkRefPointMan::GetAnalyCfg(void **ppVoid)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( m_nTempType < 0 ) return DD_ERR_ANALY_TEMPLATEINVALID;

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	return m_pActivePoint->GetAnalyCfg( ppVoid );
}

int xkRefPointMan::SetUserData(BYTE *pUserData,int nDataSize)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	return m_pActivePoint->SetUserData( pUserData,nDataSize );
}
int xkRefPointMan::GetUserData(BYTE **ppUserData,int &nDataSize)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	return m_pActivePoint->GetUserData( ppUserData,nDataSize );
}

int xkRefPointMan::DeletePoint(int nPoint)
{
	DD_AUTO_LOCK xLock(&m_RefSect);

	int nCount = (int)m_arImgRefPoint.size();
	if( nPoint < 0 || nPoint >= nCount )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	xkRefPoint *pPoint = m_arImgRefPoint[nPoint];
	if( pPoint == m_pActivePoint )
	{
		m_pActivePoint = NULL;
	}
	MySafeDelete( pPoint );
	m_arImgRefPoint.erase( m_arImgRefPoint.begin() + nPoint );

	char szTempFile[MAX_PATH] = "";
	sprintf(szTempFile,"%s\\%03d\\%03d.%s",m_szAnalyDir,m_nTempType,nPoint,DD_ANALYTEMPEXT);
	
	
	char *pTmp = NULL;
	char szParamFile[MAX_PATH] = "";
	char szMaskFile[MAX_PATH] = "";
	char szDetRtFile[MAX_PATH] = "";
	char szEdgeFile[MAX_PATH] = "";

	sprintf(szParamFile,szTempFile);
	pTmp = strrchr(szParamFile,'.');
	if( NULL != pTmp )
	{
		pTmp[1] = '\0';
	}

	sprintf(szMaskFile,"%s%s",szParamFile,DD_ANYMASKEXT);
	sprintf(szDetRtFile,"%s%s",szParamFile,DD_ANYDETRECTEXT);
#ifdef DD_NEW_EDGE
	sprintf(szEdgeFile,"%s%s.bmp",szParamFile,DD_ANYEDGEEXT);
#else
	sprintf(szEdgeFile,"%s",szParamFile,DD_ANYEDGEEXT);
#endif
	sprintf(szParamFile,"%s%s",szParamFile,DD_ANALYPARAMEXT);


	::DeleteFile(szTempFile);
	::DeleteFile(szParamFile);
	::DeleteFile(szEdgeFile);
	::DeleteFile(szDetRtFile);
	::DeleteFile(szMaskFile);


	return 0;
}



int xkRefPointMan::SetMask(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	m_pActivePoint->SetMask( pVoid );

	return 0;
}
int xkRefPointMan::DelMask(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	m_pActivePoint->SetMask( pVoid,0 );

	return 0;
}

int xkRefPointMan::SetEdgeConditionThreshold(float fThreshold)
{
	DD_AUTO_LOCK xLock(&m_RefSect);
	if( NULL == m_pActivePoint )
	{
		return DD_ERR_ANALY_POINTINVALID;
	}

	return m_pActivePoint->SetEdgeConditionThreshold( fThreshold );
}
